<?php

require( 'lib.php' );
header("Content-type: text/plain");

// Configuration header
$config = array( 'config' => array(
	'type' => "phpmysyslog-config",
	'version' => VERSION,
	'start' => getFirstDate('datetime'),
	'resets' => json_decode("[$conf_resets]"),
	'lines' => $conf_lines,
	'item' => array()
));

// Database mapping
foreach( $dbitem as $item ) {

	// name and label
	$i = array( 
		'name'=>'P'.$item['column'], 
		'label'=>$item['label'], 
		'type'=>$item['type'], 
		'autofilter'=>$item['autofilter'] );
	
	// According to type
	switch( $item['type'] ) {
	
		// Autolist: create a list by querying the database
		case 'AUTOLIST':
			$vdb = getList( $item['column'], $dbmaxlist );
			// List is too long
			if( $vdb == NULL ) {
				$i['type'] = 'PARTIALMATCH';
				$i['size'] = 12;
			// Create list
			} else {
				$v = array();	// name, label, class
				$i['type'] = 'AUTOLIST';
				foreach( $vdb as $v2 ) {
					$v[] = array( 'name'=>$v2, 'label'=>$v2, 'className'=>"c_".preg_replace('/\W/', '_',$item['column'].'_'.$v2) );
				}
				$i['values'] = $v;
			}
			break;
			
		// List: provided list of values
		case 'LIST':
			$v = array();	// name, label, class
			foreach( $item['list'] as $vn => $vv ) {
				$v[] = array( 'name'=>$vn, 'label'=>$vv['label'], 'className'=>$vv['class'] );
			};
			$i['values'] = $v;
			break;

		// ExactMatch, PartialMatch, RegexMatch, Text
		case 'EXACTMATCH':	// Search using exact match 
		case 'PARTIALMATCH':	// Search using partial match
		case 'REGEXMATCH':	// Search using regex
			$i['size'] = $item['size'];
			break;

		case 'TIMESTAMP':	// Search by interval match
		case 'TEXT':		// No search
			break;
		
		default:
			die('Unknown type '.$item['type']." for ".$item['column']);
	}
	
	// add it to configuration
	$config['config']['item'][] = $i;
}

// Send config
print json_encode($config);

/**	EXAMPLE
{"config": {
	"type": "phpmysyslog-config",	// Type of configuration
	"version": "1.0",		// Version
	"start": "2011-01-01",		// Default and Reset Start date to use (last is today) => Pstart_d
	"resets": [			// Reset buttons
		// Value is in seconds, Start is set to max(start,(now - value)) 
		{ "label":"All", "title":"Reset to max range",     "value":9999999999 },
		{ "label":"3m",  "title":"Reset to last 3 month",  "value":8035200 },
		{ "label":"1m",  "title":"Reset to last month",    "value":2678400 },
		{ "label":"2w",  "title":"Reset to last 2 weeks",  "value":1209600 },
		{ "label":"1w",  "title":"Reset to last week",     "value":604800 },
		{ "label":"3d",  "title":"Reset to last 3 days",   "value":259200 },
		{ "label":"24h", "title":"Reset to last 24 hours", "value":86400 },
		{ "label":"3h",  "title":"Reset to last 3 hours",  "value":10800 },
		{ "label":"1h",  "title":"Reset to last 1 hour",   "value":3600 }
	],
	"lines": [
		// Drop down number of lines
		{ "value":10, "default":1 },
		{ "value":20 },
		{ "value":50 },
		{ "value":100 },
		{ "value":200 },
		{ "value":500 },
		{ "value":1000 }
	],
	"item": [
		// type=TIMESTAMP, refer to top page filter about date and time interval
		{"name": "time", "label": "Timestamp", "type": "TIMESTAMP" },
		// type=LIST, drop-dow list of values, default to empty (all), 
		// all request submited using 'name', all displays using 'label' and 'class'
		{"name": "host", "label": "Host", "type": "LIST", "values":	[
			{ "name": "ramoth",   "label": "ramoth",    "class": "c_host_ramoth" },
			{ "name": "kedesch",  "label": "kedesch",   "class": "c_host_kedesch" },
			{ "name": "hebron",   "label": "hebron",    "class": "c_host_hebron" }
		]},
		{"name": "facility", "label": "Facility", "type": "LIST", "values": [
			{ "name": "auth",     "label": "auth",      "class": "c_facility_auth" },
			{ "name": "authpriv", "label": "authpriv",  "class": "c_facility_authpriv" },
			{ "name": "cron",     "label": "cron",      "class": "c_facility_cron" },
			{ "name": "daemon",   "label": "daemon",    "class": "c_facility_daemon" },
			{ "name": "ftp",      "label": "ftp",       "class": "c_facility_ftp" },
			{ "name": "kern",     "label": "kern",      "class": "c_facility_kern" },
			{ "name": "lpr",      "label": "lpr",       "class": "c_facility_lpr" },
			{ "name": "mail",     "label": "mail",      "class": "c_facility_mail" },
			{ "name": "mark",     "label": "mark",      "class": "c_facility_mark" },
			{ "name": "news",     "label": "news",      "class": "c_facility_news" },
			{ "name": "security", "label": "security",  "class": "c_facility_security" },
			{ "name": "syslog",   "label": "syslog",    "class": "c_facility_syslog" },
			{ "name": "user",     "label": "user",      "class": "c_facility_user" },
			{ "name": "uucp",     "label": "uucp",      "class": "c_facility_uucp" },
			{ "name": "local0",   "label": "local0",    "class": "c_facility_local0" },
			{ "name": "local1",   "label": "local1",    "class": "c_facility_local1" },
			{ "name": "local2",   "label": "local2",    "class": "c_facility_local2" },
			{ "name": "local3",   "label": "local3",    "class": "c_facility_local3" },
			{ "name": "local4",   "label": "local4",    "class": "c_facility_local4" },
			{ "name": "local5",   "label": "local5",    "class": "c_facility_local5" },
			{ "name": "local6",   "label": "local6",    "class": "c_facility_local6" },
			{ "name": "local7",   "label": "local7",    "class": "c_facility_local7" },
		]},
		{"name": "level", "label":    "Level", "type": "LIST", "values": [
			{ "name": "emerg",    "label": "Emergency", "class": "c_level_emerg" },
			{ "name": "alert+",   "label": "Alert -&gt; Emerg", "class": "c_level_alert" }, 
			{ "name": "alert",    "label": "Alert",     "class": "c_level_alert" },
			{ "name": "crit+",    "label": "Critical -&gt; Emerg", "class": "c_level_crit" },
			{ "name": "crit",     "label": "Critical",  "class": "c_level_crit" },
			{ "name": "err+",     "label": "Error -&gt; Emerg", "class": "c_level_err" },
			{ "name": "err",      "label": "Error",     "class": "c_level_err" },
			{ "name": "warning+", "label": "Warning -&gt; Emerg", "class": "c_level_warn" },
			{ "name": "warning",  "label": "Warning",   "class": "c_level_warn" },
			{ "name": "notice+",  "label": "Notice -&gt; Emerg", "class": "c_level_notice" },
			{ "name": "notice",   "label": "Notice",    "class": "c_level_notice" },
			{ "name": "info+",    "label": "Info -&gt; Emerg", "class": "c_level_info" },
			{ "name": "info",     "label": "Info",      "class": "c_level_info" },
			{ "name": "debug+",   "label": "Debug -&gt; Emerg", "class": "c_level_debug" },
			{ "name": "debug",    "label": "Debug",     "class": "c_level_debug" },
			{ "name": "none",     "label": "none",      "class": "c_level_none" }
		]};
		// type=PARTIALMATCH, search using partial matching (print '%' before input)
		{"name": "program", "label": "Program", "type": "PARTIALMATCH" },
		// type=EXACTMATCH, search using exact matching (print '=' before input)
		{"name": "pid", "label": "PID", "type": "EXACTMATCH" },
		// type=REGEXMATCH, serach using regex matching (print '%' before input)
		{"name": "message", "label": "Message", "type": "REGEXMATCH" }
	]
}}

*/